
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Makstanley
 */
public class Search_Flight_Controller {
    
        Connection conn = null;
        PreparedStatement p = null;
        ResultSet rs = null;
    
        
        
    private JET_Wing_MyForm view1;
    
    private ActionListener actionListener;
    
    
    public Search_Flight_Controller(JET_Wing_MyForm v){
        view1 = v;
        view1.setVisible(true);
    }
    
    
    
    public void control(){
        try{
                   Class.forName("com.mysql.jdbc.Driver").newInstance();
                    conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/jetwingdb?user=root&password=");
                    //System.out.println("passes");
        }catch (Exception e){
            System.out.println("exception in control "+e.toString());
        }
    actionListener= new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource()==view1.getSearchButtonFlight()){
                    
                    searchFlight();
                }
                else if(e.getSource()==view1.getSEARCHButtonReservation()){
                    showNewForm2();
                    
                }
                
                 else if(e.getSource()==view1.getSEARCHButtonEmployee()){
                    showNewForm1();
                    
                }
                else if(e.getSource()==view1.getCLOSEButton()){
//                    HomePage home= new HomePage();
//                    home.setVisible(true);
                 view1.dispose();
                }
                 
                   
            }
        };
    view1.getSEARCHButtonEmployee().addActionListener(actionListener);
    view1.getCLOSEButton().addActionListener(actionListener);
    view1.getSEARCHButtonReservation().addActionListener(actionListener);
    view1.getSearchButtonFlight().addActionListener(actionListener);
   
    }
    

    
    public void showNewForm1() {

        SEARCH_EMPLOYEE view = new SEARCH_EMPLOYEE();
        Search_Employee_Controller controller = new Search_Employee_Controller(view);

        controller.control();
    }


     public void showNewForm2(){
         
            SEARCH_RESERVATION view2= new SEARCH_RESERVATION();
            Search_Reservation_Controller controller2 = new Search_Reservation_Controller(view2);
            
            controller2.control();
    }

    
     
     
       public void searchFlight(){
           
            try{
                   Class.forName("com.mysql.jdbc.Driver").newInstance();
                    conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/jetwingdb?user=root&password=");
                    //System.out.println("passes");
        }catch (Exception e){
            System.out.println("exception in control "+e.toString());
        } 
           
           
           
           
         if(view1.getSearchFlightText().getText().isEmpty()){
            JOptionPane.showMessageDialog(null,"please fill search field");
        }
         
         if(view1.getsearchCombo().getSelectedIndex()==0){
              view1.getmsgLabel().setText("Enter flight name");
            //DefaultTableModel tbl = new DefaultTableModel (colname, 0);
            try{
                    
                    p = conn.prepareStatement("SELECT * FROM flights  where fName=?");                    
                    p.setString(1, view1.getSearchFlightText().getText());
                    rs = p.executeQuery();
                    //this clears table
                 view1.getModel().setRowCount(0);
                    
                while(rs.next()){
                   // System.out.println("inside");
                   view1.getModel().addRow(new String[]{
                   rs.getString("fName"),
                   rs.getString("fFrom"),
                   rs.getString("destination"),
                   rs.getString("price"),
                   rs.getString("fCapacity"),
                   rs.getString("depatureTime"),
                   rs.getString("arrivalTime"),  
                   });
                }
                //System.out.println("out");
                //flightTable.setModel(tbl);
                }catch(Exception e){
                    System.out.println("Exception "+e.getMessage());
                    JOptionPane.showMessageDialog(null, e.getMessage());
                    JOptionPane.showMessageDialog(null, "entry cannot be found");
                }
            
         }
        
         
         if(view1.getsearchCombo().getSelectedIndex()==1){
             view1.getmsgLabel().setText("Enter from");
            //DefaultTableModel tbl = new DefaultTableModel (colname, 0);
            try{
                  
                    p = conn.prepareStatement("SELECT * FROM flights  where fFrom=?");
                    p.setString(1, view1.getSearchFlightText().getText());
                    rs = p.executeQuery();

                    //this clears table
                 view1.getModel().setRowCount(0);
                     
                    
                while(rs.next()){
                   view1.getModel().addRow(new String[]{
                   rs.getString("fName"),
                   rs.getString("fFrom"),
                   rs.getString("destination"),
                   rs.getString("price"),
                   rs.getString("fCapacity"),
                   rs.getString("depatureTime"),
                   rs.getString("arrivalTime"),          
                   });
                }
                //flightTable.setModel(tbl);
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, e.getMessage());
                    JOptionPane.showMessageDialog(null, "entry cannot be found");
                }
            
         }
         
         
         
         if(view1.getsearchCombo().getSelectedIndex()==2){
            //view1.getmsgLabel().setText("HR, FIN,IT, BB, SC, Foods, ENG, CD,LAB, QTY, SFTY");
            //DefaultTableModel tbl = new DefaultTableModel (colname, 0);
            try{
                    p = conn.prepareStatement("SELECT * FROM flights  where destination=?");
                    p.setString(1, view1.getSearchFlightText().getText());
                    rs = p.executeQuery();

                 //this clears table
                 view1.getModel().setRowCount(0);
                        
                    
                while(rs.next()){
                   view1.getModel().addRow(new String[]{
                   rs.getString("fName"),
                   rs.getString("fFrom"),
                   rs.getString("destination"),
                   rs.getString("price"),
                   rs.getString("fCapacity"),
                   rs.getString("depatureTime"),
                   rs.getString("arrivalTime"), 
                   });
                }
                //flightTable.setModel(tbl);
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, e.getMessage());
                    JOptionPane.showMessageDialog(null, "entry cannot be found");
                }
            
         }
         
         if(view1.getsearchCombo().getSelectedIndex()==3){
            //DefaultTableModel tbl = new DefaultTableModel (colname, 0);
            try{
                    p = conn.prepareStatement("SELECT * FROM flights  where price=?");
                    p.setString(1, view1.getSearchFlightText().getText());
                    rs = p.executeQuery();

                    
                //this clears table
                 view1.getModel().setRowCount(0);     
                    
                while(rs.next()){
                  view1.getModel().addRow(new String[]{
                   rs.getString("fName"),
                   rs.getString("fFrom"),
                   rs.getString("destination"),
                   rs.getString("price"),
                   rs.getString("fCapacity"),
                   rs.getString("depatureTime"),
                   rs.getString("arrivalTime"),
                   });
                }
                //flightTable.setModel(tbl);
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, e.getMessage());
                     JOptionPane.showMessageDialog(null, "entry cannot be found");
                }
           
         }
           
     
        
         if(view1.getsearchCombo().getSelectedIndex()==4){
            //DefaultTableModel tbl = new DefaultTableModel (colname, 0);
            try{
                    p = conn.prepareStatement("SELECT * FROM flights  where fCapacity=?");
                    p.setString(1, view1.getSearchFlightText().getText());
                    rs = p.executeQuery();

                  //this clears table
                 view1.getModel().setRowCount(0);
                       
                    
                while(rs.next()){
                   view1.getModel().addRow(new String[]{
                   rs.getString("fName"),
                   rs.getString("fFrom"),
                   rs.getString("destination"),
                   rs.getString("price"),
                   rs.getString("fCapacity"),
                   rs.getString("depatureTime"),
                   rs.getString("arrivalTime"),
                   });
                }
                //flightTable.setModel(tbl);
                }catch(Exception e){
                     JOptionPane.showMessageDialog(null, e.getMessage());
                     JOptionPane.showMessageDialog(null, "entry cannot be found");
                }
           
         } 
         
            
         
         if(view1.getsearchCombo().getSelectedIndex()==5){
            //DefaultTableModel tbl = new DefaultTableModel (colname, 0);
            try{
                    p = conn.prepareStatement("SELECT * FROM flights  where depatureTime=?");
                    p.setString(1, view1.getSearchFlightText().getText());
                    rs = p.executeQuery();

                 //this clears table
                 view1.getModel().setRowCount(0);
                        
                    
                while(rs.next()){
                   view1.getModel().addRow(new String[]{
                   rs.getString("fName"),
                   rs.getString("fFrom"),
                   rs.getString("destination"),
                   rs.getString("price"),
                   rs.getString("fCapacity"),
                   rs.getString("depatureTime"),
                   rs.getString("arrivalTime"),
                   });
                }
                //flightTable.setModel(tbl);
                }catch(Exception e){
                    //JOptionPane.showMessageDialog(null, e.getMessage());
                     JOptionPane.showMessageDialog(null, "entry cannot be found");
                }
           
         } 
         
         
         
          if(view1.getsearchCombo().getSelectedIndex()==6){
            //DefaultTableModel tbl = new DefaultTableModel (colname, 0);
            try{
                    p = conn.prepareStatement("SELECT * FROM flights  where arrivalTime=?");
                    p.setString(1, view1.getSearchFlightText().getText());
                    rs = p.executeQuery();
                
                    //this clears table
                 view1.getModel().setRowCount(0);
                    
                                     
                while(rs.next()){
                   view1.getModel().addRow(new String[]{
                   rs.getString("fName"),
                   rs.getString("fFrom"),
                   rs.getString("destination"),
                   rs.getString("price"),
                   rs.getString("fCapacity"),
                   rs.getString("depatureTime"),
                   rs.getString("arrivalTime"),
                   });
                }
                //flightTable.setModel(tbl);
                }catch(Exception e){
                    //JOptionPane.showMessageDialog(null, e.getMessage());
                    JOptionPane.showMessageDialog(null, "entry cannot be found");
                }
           
         }   
         
    }
     
}